/**
 * 
 */
package cn.egame.extraction.timer;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.log4j.Logger;

import cn.egame.common.util.Utils;
import cn.egame.extraction.FileSynchronizeSchduler;
import cn.egame.extraction.thread.ExtractionThreadBak;
import cn.egame.extraction.util.ConstVar_EFS;
import cn.egame.extraction.util.ExtractionInit;

/**
 * Description 非定时器启动任务
 * 
 * @ClassName ExtractionStart
 * 
 * @Copyright 炫彩互动
 * 
 * @Project egame.mobile.extraction
 * 
 * @Author Administrator
 * 
 * @Create Date 2014-6-12
 * 
 * @Modified by none
 * 
 * @Modified Date
 */

public class ExtractionStart {
	private static final Logger logger = Logger
			.getLogger(ExtractionStart.class);
	private static Logger errorSyncIdLog = Logger.getLogger("errorSyncId");
	public static AtomicInteger number = new AtomicInteger(0);
	public static LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>(ConstVar_EFS.BLOCK_SIZE);

	public static void main(String[] args) {
		try {
		    Utils.initLog4j();
			// 1.初始化
			ExtractionInit.init();
			if (ExtractionInit.efsInfoList.size() == 0) {
				while (true) {
					Thread.sleep(1000);
					ExtractionInit.writerBack(ExtractionInit.writeMaxId, ExtractionInit.readMaxId, ExtractionInit.dayStr);
					ExtractionInit.init();
					if (ExtractionInit.efsInfoList.size() != 0) {

						break;
					}

				}
			}
            //定时同步程序
            new Thread(new FileSynchronizeSchduler()).start();
			// 2.多线程去处理
			ThreadPoolExecutor threadPool = new ThreadPoolExecutor(
					ConstVar_EFS.CORE_POOL_SIZE,
					ConstVar_EFS.MAXIMUM_POOL_SIZE,
					ConstVar_EFS.KEEP_ALIVE_TIME, TimeUnit.SECONDS, queue,
					new ThreadPoolExecutor.CallerRunsPolicy());

            for (int i = 0; i < ExtractionInit.efsInfoList.size(); i++) {

                threadPool.execute(new ExtractionThreadBak(ExtractionInit.efsInfoList.get(i)));

            }
            while (true) {
                Thread.sleep(100);
                TimeProcess();//定时同步程序
                if (number.get() >= ExtractionInit.efsInfoList.size()) {
                    if (number.get() > ExtractionInit.efsInfoList.size()) {
                        errorSyncIdLog.error("num=" + number.get() + "大于ExtractionInit.efsInfoList.size()： "
                                + ExtractionInit.efsInfoList.size());
                    }
                    // 初始化入max_id_txt;
                    // logger.info("writeMaxId: "+ExtractionInit.writeMaxId+" readMaxId: "+ExtractionInit.readMaxId);
                    ExtractionInit.writerBack(ExtractionInit.writeMaxId, ExtractionInit.readMaxId, ExtractionInit.dayStr);
                    // 重置number
                    number.set(0);
                    // 1.初始化
                    ExtractionInit.init();
                    // 2.多线程处理
                    if (ExtractionInit.efsInfoList.size() == 0) {
                        while (true) {
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                logger.error(e.getMessage(), e);
                            }
                            ExtractionInit.writerBack(ExtractionInit.writeMaxId, ExtractionInit.readMaxId, ExtractionInit.dayStr);
                            ExtractionInit.init();
                            if (ExtractionInit.efsInfoList.size() != 0) {

                                break;
                            }

                        }
                    }
                    for (int j = 0; j < ExtractionInit.efsInfoList.size(); j++) {
                        try {
                            Thread.sleep(ConstVar_EFS.MULTI_SLEEP_TIME);
                        } catch (InterruptedException e) {
                            logger.error(e.getMessage(), e);
                        }
                        threadPool.execute(new ExtractionThreadBak(ExtractionInit.efsInfoList.get(j)));
                    }
                }
            }
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}
   /**
    * @Description: 定时同步程序 
    *    
    * @return void     
    * @throws
    * @Author yujm
    * @Create Date  2014年12月26日
    * @Modified by none
    * @Modified Date
    */
    private static void TimeProcess() {
        if (FileSynchronizeSchduler.flag.get()) {
            FileSynchronizeSchduler schduler = new FileSynchronizeSchduler();
            Thread thread = new Thread(schduler);
            thread.start();
        }
    }
}
